/* PROJECT : http://code.google.com/p/gtk-study-korea/
 *
 * SUB_PROJECT : BJ_QUIZ
 *
 * FILE : main.c
 */

#include <gtk/gtk.h>

#include "headers.h"

/******************************************
 * 상수
 ******************************************/
#define SVN_VERSION  46

/******************************************
 * 함수
 ******************************************/
static void destroy( GtkWidget *widget, gpointer   data );
static void on_disp_area_expose_event(GtkWidget *widget, _WIN_MAIN *wm);

/******************************************
 * 입력 :
 * 사용 :
 * 출력 :
 * 설명 :
 *     메인
 ******************************************/
int main( int   argc, char *argv[] )
{
	GtkBuilder *bd_main;

	gchar msg[40];

	// 변수 초기화
	WM.st_id = 0;
	F_LOG = 0;

	F_LOG = (FILE *)(g_fopen("log.txt", "a+") );

	gtk_init (&argc, &argv);

	bd_main = gtk_builder_new();
	if( !gtk_builder_add_from_file( bd_main, "main.glade", NULL) )
	{
		g_fprintf(F_LOG, "\nmain.glade를 열 수 없습니다.");
		fclose(F_LOG);
		exit(1);
	}
	
	// 위젯 생성
	WM.main = GTK_WIDGET( gtk_builder_get_object( bd_main, "main") );
	g_sprintf(msg, "BJ_QUIZ [SVN %d]", SVN_VERSION);
	gtk_window_set_title( GTK_WINDOW(WM.main), msg);

	WM.disp_area = GTK_WIDGET( gtk_builder_get_object( bd_main, "disp_area") );

	WM.display = GTK_WIDGET( gtk_builder_get_object( bd_main, "display") );
	gtk_image_set_from_file( GTK_IMAGE(WM.display), "sample_img.jpg");

	WM.status = GTK_WIDGET( gtk_builder_get_object( bd_main, "status") );
	WM.st_id = gtk_statusbar_get_context_id( GTK_STATUSBAR(WM.status), "시작");
	g_sprintf(msg, "Display %d x %d", WM.display->requisition.width, WM.display->requisition.height);
	gtk_statusbar_push( GTK_STATUSBAR(WM.status), WM.st_id, msg);

	// 위젯 결합

	// 시그널 연결
	gtk_builder_connect_signals( bd_main, NULL);

	g_signal_connect (G_OBJECT (WM.main), "destroy", G_CALLBACK (destroy), NULL);

	g_signal_connect_after (G_OBJECT (WM.main), "expose-event", G_CALLBACK (on_disp_area_expose_event), NULL);

	/*  TODO: type some code */

	g_object_unref( bd_main);

	gtk_widget_show_all(WM.main);
	
	gtk_main ();
	
	return 0;
}

/******************************************
 * 입력 :
 * 사용 :
 * 출력 :
 * 설명 :
 *     종료 함수
 ******************************************/
static void destroy( GtkWidget *widget, gpointer   data )
{
	if(F_LOG)
	{
		g_fprintf(F_LOG,"\n종료");
		g_fprintf(F_LOG,"\n=====================");
		fclose(F_LOG);
	}

	gtk_main_quit ();
}

/******************************************
 * 입력 :
 * 사용 :
 * 출력 :
 * 설명 :
 *     종료 함수
 ******************************************/
static void on_disp_area_expose_event(GtkWidget *widget, _WIN_MAIN *wm)
{
	GdkPixbuf *pixbuf;

	gchar buf[40];

	pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(WM.display) );
	if(pixbuf == NULL)
	{
		return;
	}

	pixbuf = gdk_pixbuf_scale_simple( pixbuf, WM.disp_area->allocation.width - 20, WM.disp_area->allocation.height - 20, GDK_INTERP_BILINEAR);

	gtk_image_set_from_pixbuf( GTK_IMAGE(WM.display), pixbuf);

	if(WM.st_id)
	{
		g_sprintf(buf, "Display %d x %d", WM.disp_area->allocation.width, WM.disp_area->allocation.height);

		gtk_statusbar_pop( GTK_STATUSBAR(WM.status), WM.st_id);
		gtk_statusbar_push( GTK_STATUSBAR(WM.status), WM.st_id, buf);
	}
}

/*******************************************/

